
import cn.hutool.core.collection.ArrayIter;
import com.cloud.layuimini.LayuiminiApplication8080;
import com.cloud.layuimini.dao.MenuDao;
import com.cloud.layuimini.entity.MenuEntity;
import com.cloud.layuimini.service.MenuService;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = LayuiminiApplication8080.class)// 与上文启动类-极简版对应
public class JpaDemo {

    @Autowired
    MenuDao menuDao;
    @Autowired
    MenuService menuService;

    @Test
    public void saveTest() throws Exception {
        MenuEntity s = new MenuEntity();
        s.setTitle("title1");
        s.setHref("javascript::");
        menuDao.save(s);
    }

    @Test
    public void saveAll() {
        ArrayList<MenuEntity> s = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            MenuEntity entity = new MenuEntity();
            entity.setTitle("title1" + i);
            entity.setHref("javascript::" + i);
            s.add(entity);
        }
        menuDao.saveAll(s);
    }


    @Test
    public void name() {

        List<Object[]> title = menuDao.selectCustom("%title%");
        for (Object[] objects : title) {
            for (Object object : objects) {
                System.out.println(String.valueOf(object));
            }
        }
        System.out.println(title);
    }

    @Test
    public void SpecificationTest() {
        //匿名内部类
        /**
         * 自定义查询条件
         *      1.实现Specification接口（提供泛型：查询的对象类型）
         *      2.实现toPredicate方法（构造查询条件）
         *      3.需要借助方法参数中的两个参数（
         *          root：获取需要查询的对象属性
         *          CriteriaBuilder：构造查询条件的，内部封装了很多的查询条件（模糊匹配，精准匹配）
         *       ）
         */
        Specification<MenuEntity> spec = new Specification<MenuEntity>() {
            @Override
            public Predicate toPredicate(Root<MenuEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<Object> title = root.get("title");//
                Path<Object> href = root.get("href");//
                //构造查询
                //1.构造客户名的精准匹配查询
                Predicate p1 = cb.equal(title, "黑马程序员");//第一个参数，path（属性），第二个参数，属性的取值
                //2..构造所属行业的精准匹配查询
                Predicate p2 = cb.equal(href, "hangz");
                //3.将多个查询条件组合到一起：组合（满足条件一并且满足条件二：与关系，满足条件一或满足条件二即可：或关系）
                // cb.or();//以或的形式拼接多个查询条件
                return cb.and(p1, p2);
            }
        };
        Optional<MenuEntity> one = menuDao.findOne(spec);
        System.out.println(one);
    }

    /**
     * 不好用
     */
    @Test
    public void queryExample() {
        MenuEntity entity = new MenuEntity();
        entity.setTitle("2");
        entity.setHref("javascript::" );
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("title", ExampleMatcher.GenericPropertyMatchers.startsWith())//模糊查询匹配开头，即{username}%
                .withMatcher("href" ,ExampleMatcher.GenericPropertyMatchers.contains());//全部模糊查询，即%{address}%
        Example<MenuEntity> example = Example.of(entity ,matcher);

        List<MenuEntity> all = menuDao.findAll(example);

        for (MenuEntity menuEntity : all) {
            System.out.println(menuEntity);
        }

        List<MenuEntity> all1 = menuDao.findAll(Example.of(entity));
    }

    @Test
    public void findAllTest() {
        List<MenuEntity> all = menuDao.findAll();
        for (MenuEntity entity : all) {
            System.out.println(entity);
        }
    }
}
